package net.wsfcp.android.ui.view.my.fragment;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;

import net.wsfcp.android.R;
import net.wsfcp.android.base.BaseFragment;
import net.wsfcp.android.bean.resultbean.UserBean;
import net.wsfcp.android.bean.resultbean.PublicKeyBean;
import net.wsfcp.android.greendao.utils.UserBeanUtil;
import net.wsfcp.android.http.mvp.contract.MyContract;
import net.wsfcp.android.event.LoginEvent;
import net.wsfcp.android.http.mvp.presenter.MyPresenter;
import net.wsfcp.android.ui.view.my.activity.AddressActivity;
import net.wsfcp.android.ui.view.my.activity.BillListActivity;
import net.wsfcp.android.ui.view.my.activity.ChangePasswordActivity;
import net.wsfcp.android.ui.view.my.activity.InfoActivity;
import net.wsfcp.android.ui.view.my.activity.LoginActivity;
import net.wsfcp.android.ui.view.my.activity.OrderListActivity;
import net.wsfcp.android.utils.SpUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;

public class MyFragment extends BaseFragment<MyPresenter> implements MyContract.MyView {
    @BindView(R.id.ll_setting_more)
    LinearLayout llSettingMore;
    @BindView(R.id.ll_setting)
    LinearLayout llSetting;
    @BindView(R.id.ll_dingdan)
    LinearLayout llDingdan;
    @BindView(R.id.ll_zhangdan)
    LinearLayout llZhangdan;
    @BindView(R.id.ll_info)
    LinearLayout llInfo;
    @BindView(R.id.ll_password)
    LinearLayout llPassword;
    @BindView(R.id.ll_address)
    LinearLayout llAddress;

    @BindView(R.id.img_more)
    ImageView imgMore;
    @BindView(R.id.ll_head)
    LinearLayout llHead;
    @BindView(R.id.img_head)
    ImageView imgHead;
    @BindView(R.id.txt_username)
    TextView txtUsername;

    @BindView(R.id.txt_logout)
    TextView txtLogout;

    boolean isLogin = false;


    @Override
    protected MyPresenter loadPresenter() {
        return new MyPresenter();
    }

    @Override
    protected void initData() {
        mPresenter.getPublicKey();


        if (SpUtils.getInstance().getLoginState() == 1 && !isLogin) {
            mPresenter.autoLogin(SpUtils.getInstance().getUserName(), SpUtils.getInstance().getEncrypt());
        }
    }

    @Override
    protected void initListener() {
        llHead.setOnClickListener(this);
        llDingdan.setOnClickListener(this);
        llZhangdan.setOnClickListener(this);
        llSetting.setOnClickListener(this);
        llInfo.setOnClickListener(this);
        llPassword.setOnClickListener(this);
        llAddress.setOnClickListener(this);
        txtLogout.setOnClickListener(this);
    }

    @Override
    protected void initView() {

        if (isLogin()) {
            txtLogout.setVisibility(View.VISIBLE);

            imgHead.setImageResource(R.mipmap.index_bg_02);
            txtUsername.setText(SpUtils.getInstance().getUserName());
        } else {
            txtLogout.setVisibility(View.GONE);

            imgHead.setImageResource(R.mipmap.ic_launcher);
            txtUsername.setText(R.string.fragment_my_login);
        }


    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_my;
    }

    @Override
    protected void otherViewClick(View view) {

        switch (view.getId()) {
            case R.id.txt_logout:
                new AlertDialog.Builder(getContext())
                        .setMessage("确定要退出登录吗?")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {

                                dialogInterface.dismiss();

                                SpUtils.getInstance().put(SpUtils.LOGIN_STATE, 0);
                                EventBus.getDefault().post(new LoginEvent(0));

                            }
                        }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                        dialogInterface.dismiss();
                    }
                }).create().show();
                break;
            case R.id.ll_setting:
                if (llSettingMore.getVisibility() == View.VISIBLE) {
                    llSettingMore.setVisibility(View.GONE);
                    //箭头旋转
                    imgMore.setPivotX(imgMore.getWidth() / 2);
                    imgMore.setPivotY(imgMore.getHeight() / 2);//支点在图片中心
                    imgMore.setRotation(0);

                } else {
                    llSettingMore.setVisibility(View.VISIBLE);
                    //箭头旋转
                    imgMore.setPivotX(imgMore.getWidth() / 2);
                    imgMore.setPivotY(imgMore.getHeight() / 2);//支点在图片中心
                    imgMore.setRotation(90);
                }
                break;
            case R.id.ll_head:
                if (isLogin()) {
                } else {
                    startActivity(LoginActivity.class);
                }

                break;

            case R.id.ll_dingdan:

                if (isLogin()) {
                    startActivity(OrderListActivity.class);
                } else {
                    startActivity(LoginActivity.class);
                }

                break;
            case R.id.ll_zhangdan:
                if (isLogin()) {
                    startActivity(BillListActivity.class);
                } else {
                    startActivity(LoginActivity.class);
                }

                break;
            case R.id.ll_info:
                if (isLogin()) {
                    startActivity(InfoActivity.class);
                } else {
                    startActivity(LoginActivity.class);
                }

                break;
            case R.id.ll_password:
                if (isLogin()) {
                    startActivity(ChangePasswordActivity.class);
                } else {
                    startActivity(LoginActivity.class);
                }

                break;
            case R.id.ll_address:
                if (isLogin()) {
                    startActivity(AddressActivity.class);
                } else {
                    startActivity(LoginActivity.class);
                }


                break;


        }


    }

    public boolean isLogin() {

        return isLogin;
    }


    @Override
    public void getPublicKeySuccess(PublicKeyBean publicKeyBean) {
        SpUtils.getInstance().put(SpUtils.PUBLIC_KEY, publicKeyBean.getPublic_key());

    }

    @Override
    public void getPublicKeyFail(String failMsg) {
        toast(failMsg);
    }

    @Override
    public void autoLoginSuccess(UserBean bean) {
        UserBeanUtil userBeanUtil = new UserBeanUtil(getContext());
        userBeanUtil.updateUser(bean);


        SpUtils.getInstance().saveUserInfo(bean);
        SpUtils.getInstance().put(SpUtils.LOGIN_STATE, 1);
        EventBus.getDefault().post(new LoginEvent(1));
        toast("登录成功");

    }

    @Override
    public void autoLoginFail(String failMsg) {
        toast(failMsg);
    }


    //接收订阅的事件
    @Subscribe(threadMode = ThreadMode.POSTING)
    public void login(LoginEvent event) {
        switch (event.getCode()) {
            case 0:   // 退出登录
                txtLogout.setVisibility(View.GONE);

                imgHead.setImageResource(R.mipmap.ic_launcher);
                txtUsername.setText(R.string.fragment_my_login);
                isLogin = false;
                break;
            case 1:   // 登录
                txtLogout.setVisibility(View.VISIBLE);
                UserBeanUtil userBeanUtil = new UserBeanUtil(getContext());
                UserBean userBean = userBeanUtil.getUserInfo(SpUtils.getInstance().getUserId());
                if (!TextUtils.isEmpty(userBean.getPortrait_uri())) {
                    Glide.with(this).load(userBean.getPortrait_uri()).apply(new RequestOptions().circleCrop()).into(imgHead);
                }


                txtUsername.setText(SpUtils.getInstance().getUserName());


                isLogin = true;
                break;
            case 2:   // 忘记密码
                txtLogout.setVisibility(View.GONE);

                imgHead.setImageResource(R.mipmap.ic_launcher);
                txtUsername.setText(R.string.fragment_my_login);
                isLogin = false;
                Intent intent = new Intent(getContext(), LoginActivity.class);
                intent.putExtra("forget", true);
                startActivity(intent);
                break;

        }

    }
}